﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace YodaMaster.Models
{
    public class Schedule_Controller_funcs
    {

        public void choose_from_three(ref Schedule current)
        {
            int n1, n2, n3;
            int p = current.course_counter;
            
            for (n1 = current.index_lec[p]; n1 < current.Counters.ElementAt(p).lecgroups_count; n1++)
            {
                current.cycle = 0;
                for (n2 = current.index_tut[p]; n2 < current.Counters.ElementAt(p).tutgroups_count; n2++)
                {
                    for (n3 = current.index_lab[p]; n3 < current.Counters.ElementAt(p).labgroups_count; n3++)
                    {
                        current.index_lab[p]++;
                        break;
                    }
                    if (current.index_lab[p] >= current.Counters.ElementAt(p).labgroups_count)
                    {
                        current.index_lab[p] = 0;
                        current.index_tut[p]++;
                        break;
                    }
                    break;
                }
                if (current.index_tut[p] >= current.Counters.ElementAt(p).tutgroups_count)
                {
                    current.index_tut[p] = 0;
                    current.index_lec[p]++;
                    break;
                }
                break;
            }
            if (current.index_lec[p] >= current.Counters.ElementAt(p).lecgroups_count)
            {
                current.index_lec[p] = 0;
                current.cycle = 1;
                if (current.course_counter > 0)
                    current.course_counter--;
            }
            if (current.cycle == 0 && current.course_counter < current.parameters.length - 1)
                current.course_counter = current.parameters.length - 1;
        }

        public void choose_from_two_no_lab(ref Schedule current)
        {
            int n1, n2;
            int p = current.course_counter;

            for (n1 = current.index_lec[p]; n1 < current.Counters.ElementAt(p).lecgroups_count; n1++)
            {
                current.cycle = 0;
                for (n2 = current.index_tut[p]; n2 < current.Counters.ElementAt(p).tutgroups_count; n2++)
                {
                    current.index_tut[p]++;
                    break;
                }
                if (current.index_tut[p] >= current.Counters.ElementAt(p).tutgroups_count)
                {
                    current.index_tut[p] = 0;
                    current.index_lec[p]++;
                    break;
                }
                break;
            }

            if (current.index_lec[p] >= current.Counters.ElementAt(p).lecgroups_count)
            {
                current.index_lec[p] = 0;
                current.cycle = 1;
                if (current.course_counter > 0)
                    current.course_counter--;
            }
            if (current.cycle == 0 && current.course_counter < current.parameters.length - 1)
                current.course_counter = current.parameters.length - 1;
        }

        public void choose_from_two_no_tut(ref Schedule current)
        {
            int n1, n2;
            int p = current.course_counter;
            for (n1 = current.index_lec[p]; n1 < current.Counters.ElementAt(p).lecgroups_count; n1++)
            {
                current.cycle = 0;
                for (n2 = current.index_lab[p]; n2 < current.Counters.ElementAt(p).labgroups_count; n2++)
                {
                    current.index_lab[p]++;
                    break;
                }
                if (current.index_lab[p] >= current.Counters.ElementAt(p).labgroups_count)
                {
                    current.index_lab[p] = 0;
                    current.index_lec[p]++;
                    break;
                }
                break;
            }

            if (current.index_lec[p] >= current.Counters.ElementAt(p).lecgroups_count)
            {
                current.index_lec[p] = 0;
                current.cycle = 1;
                if (current.course_counter > 0)
                    current.course_counter--;
            }
            if (current.cycle == 0 && current.course_counter < current.parameters.length - 1)
                current.course_counter = current.parameters.length - 1;
        }

        public void choose_from_labs(ref Schedule current)
        {
            int n1;
            int p = current.course_counter;
            for (n1 = current.index_lab[p]; n1 < current.Counters.ElementAt(p).labgroups_count; n1++)
            {
                current.cycle = 0;
                current.index_lab[p]++;
                break;
            }

            if (current.index_lab[p] >= current.Counters.ElementAt(p).labgroups_count)
            {
                current.index_lab[p] = 0;
                current.cycle = 1;
                if (current.course_counter > 0)
                    current.course_counter--;
            }
            if (current.cycle == 0 && current.course_counter < current.parameters.length - 1)
                current.course_counter = current.parameters.length - 1;
        }

        public void choose_from_tutorials(ref Schedule current)
        {
            int n1;
            int p = current.course_counter;

            for (n1 = current.index_tut[p]; n1 < current.Counters.ElementAt(p).tutgroups_count; n1++)
            {
                current.cycle = 0;
                current.index_tut[p]++;
                break;
            }

            if (current.index_tut[p] >= current.Counters.ElementAt(p).tutgroups_count)
            {
                current.index_tut[p] = 0;
                current.cycle = 1;
                if (current.course_counter > 0)
                    current.course_counter--;
            }
            if (current.cycle == 0 && current.course_counter < current.parameters.length - 1)
                current.course_counter = current.parameters.length - 1;
        }
        public void choose_from_lectures(ref  Schedule current)
        {
            int n1;
            int p = current.course_counter;
            for (n1 = current.index_lec[p]; n1 < current.Counters.ElementAt(p).lecgroups_count; n1++)
            {
                current.cycle = 0;
                current.index_lec[p]++;
                break;
            }

            if (current.index_lec[p] >= current.Counters.ElementAt(p).lecgroups_count)
            {
                current.index_lec[p] = 0;
                current.cycle = 1;
                if (current.course_counter > 0)
                    current.course_counter--;
            }
            if (current.cycle == 0 && current.course_counter < current.parameters.length - 1)
                current.course_counter = current.parameters.length - 1;

        }

       public void AddCoursesToSchedule(ref Schedule current)
        {
            current.cycle = 1;
            int count = -1;
            foreach (Course course in current.SchedCourses.Keys)
            {
                var leclist_tmp = (from c in current.groups where c.type == "Lecture" select c);
                var leclist = (from c in leclist_tmp where c.id == course.number select c).ToList();
                var tutlist_tmp = (from c in current.groups where c.type == "Tutorial" select c);
                var tutlist = (from c in tutlist_tmp where c.id == course.number select c).ToList();
                var lablist_tmp = (from c in current.groups where c.type == "Lab" select c);
                var lablist = (from c in lablist_tmp where c.id == course.number select c).ToList();



                count++;
                if (course.lectureHours > 0)                                                      
                {
                         var tmp = leclist.ElementAt(current.index_lec[count]).groupNumber;
                         var group = (from c in course.lectureList where c.groupNumber == tmp select c).Single();
                     
 
                    current.schedule.Add(new Group_Course { Group = group, course = course });
                }
                if (course.tutorialHours > 0)
                {
                    var tmp = tutlist.ElementAt(current.index_tut[count]).groupNumber;
                    var group = (from c in course.tutorialList where c.groupNumber == tmp select c).Single();


                    current.schedule.Add(new Group_Course { Group = group, course = course });
                }
                if (course.labHours > 0)
                {
                    var tmp = lablist.ElementAt(current.index_lab[count]).groupNumber;
                    var group = (from c in course.labList where c.groupNumber == tmp select c).Single();

                    current.schedule.Add(new Group_Course { Group = group, course = course });
                }
            }

        }

      public   void CheckOverlapGroups(ref  Schedule current)
        {
            int flag = 0;
            foreach (var item in current.overlap_groups)
            {
                flag = 0;
                foreach (var item2 in current.schedule)
                {
                    if (item.groupNumber1.Equals(item2.Group.groupNumber) && item.id1.Equals(item2.course.number))
                        flag = 1;
                    if (flag == 1 && item.groupNumber2.Equals(item2.Group.groupNumber) && item.id2.Equals(item2.course.number))
                    {
                        current.Type = YodaMaster.Models.Schedule.Schedule_Type.Overlap;
                        item2.overlap = true;
                        foreach (var item3 in current.schedule)
                        {
                            if (item.groupNumber1.Equals(item3.Group.groupNumber) && item.id1.Equals(item3.course.number))
                                item3.overlap = true;
                        }
                        break;

                    }
                }
            }
        }
    }
}